package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.dto.LaneStageCodePageDTO;
import com.xbongbong.paas.pojo.dto.LaneVersionListDTO;
import com.xbongbong.paas.pojo.vo.LaneCountListVO;
import com.xbongbong.paas.pojo.vo.LaneVersionListVO;
import com.xbongbong.paas.pojo.vo.PaasFormLaneDataEsListVO;
import com.xbongbong.paas.service.stage.StageLaneViewService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;
/**
 * @Author mingliang
 * @Description 泳道视图
 * @Date 2021/12/6 19:05
 **/
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/laneView")
public class StageLaneViewController {

    @Resource
    private StageLaneViewService stageLaneViewService;

    /**
     * @Author mingliang
     * @Description 泳道获得版本列表
     * @Date 2021/11/25 18:59
     * @param laneVersionListDTO
     * @param br
     **/
    @RequestMapping(value = "/version/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String laneVersionList(@RequestBody @Valid LaneVersionListDTO laneVersionListDTO, BindingResult br) {
        XbbResponse<LaneVersionListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LaneVersionListVO laneVersionListVO = stageLaneViewService.laneVersionList(laneVersionListDTO);
                response = new XbbResponse<>(laneVersionListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * @Author mingliang
     * @Description 泳道获得各阶段
     * @Date 2021/11/25 11:21
     * @param laneStageCodePageDTO
     * @param br
     **/
    @RequestMapping(value = "/customerStage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String laneCustomerStage(@RequestBody @Valid LaneStageCodePageDTO laneStageCodePageDTO, BindingResult br) {
        XbbResponse<LaneCountListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(laneStageCodePageDTO.getSaasMark(), laneStageCodePageDTO.getBusinessType(), laneStageCodePageDTO.getMenuId(), laneStageCodePageDTO.getDistributorMark());
                LaneCountListVO laneCountListVO = stageLaneViewService.laneCustomerStage(laneStageCodePageDTO);
                response = new XbbResponse<>(laneCountListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * @Author mingliang
     * @Description 获得阶段视图所需的表单解释以及是否展示标题
     * @Date 2021/12/16 10:48
     * @param formDataListDTO
     * @param br
     **/
    @RequestMapping(value = "/getLaneStageInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getLaneStageInfo(@RequestBody @Valid FormDataListDTO formDataListDTO, BindingResult br) {
        XbbResponse<LaneCountListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(formDataListDTO.getSaasMark(), formDataListDTO.getBusinessType(), formDataListDTO.getMenuId(), formDataListDTO.getDistributorMark());
                LaneCountListVO laneCountListVO = stageLaneViewService.getLaneStageInfo(formDataListDTO);
                response = new XbbResponse<>(laneCountListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @Author mingliang
     * @Description 销售机会获得各个阶段
     * @Date 2021/11/28 15:11
     * @param laneStageCodePageDTO
     * @param br
     **/
    @RequestMapping(value = "/opportunityStage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String laneOpportunityStage(@RequestBody @Valid LaneStageCodePageDTO laneStageCodePageDTO, BindingResult br) {
        XbbResponse<LaneCountListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(laneStageCodePageDTO.getSaasMark(), laneStageCodePageDTO.getBusinessType(), laneStageCodePageDTO.getMenuId(), laneStageCodePageDTO.getDistributorMark());
                LaneCountListVO laneCountListVO = stageLaneViewService.laneOpportunityStage(laneStageCodePageDTO);
                response = new XbbResponse<>(laneCountListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    @RequestMapping(value = "/clueStage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String laneClueStage(@RequestBody @Valid LaneStageCodePageDTO laneStageCodePageDTO, BindingResult br) {
        XbbResponse<LaneCountListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(laneStageCodePageDTO.getSaasMark(), laneStageCodePageDTO.getBusinessType(), laneStageCodePageDTO.getMenuId(), laneStageCodePageDTO.getDistributorMark());
                LaneCountListVO laneCountListVO = stageLaneViewService.laneClueStage(laneStageCodePageDTO);
                response = new XbbResponse<>(laneCountListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/paasStage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String lanePaasStage(@RequestBody @Valid LaneStageCodePageDTO laneStageCodePageDTO, BindingResult br) {
        XbbResponse<LaneCountListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(laneStageCodePageDTO.getSaasMark(), laneStageCodePageDTO.getBusinessType(), laneStageCodePageDTO.getMenuId(), laneStageCodePageDTO.getDistributorMark());
                LaneCountListVO laneCountListVO = stageLaneViewService.lanePaasStage(laneStageCodePageDTO);
                response = new XbbResponse<>(laneCountListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取泳道数据列表与统计数据
     * @param formDataListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/info/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String laneCustomerList(@RequestBody @Valid FormDataListDTO formDataListDTO, BindingResult br) {
        XbbResponse<PaasFormLaneDataEsListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(formDataListDTO.getSaasMark(), formDataListDTO.getBusinessType(), formDataListDTO.getMenuId(), formDataListDTO.getDistributorMark());
                PaasFormLaneDataEsListVO formDataListVO = stageLaneViewService.laneViewList(formDataListDTO);
                response = new XbbResponse<>(formDataListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取泳道数据列表与统计数据
     * @param formDataListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/top/button", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String topButton(@RequestBody @Valid FormDataListDTO formDataListDTO, BindingResult br) {
        XbbResponse<PaasFormLaneDataEsListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(formDataListDTO.getSaasMark(), formDataListDTO.getBusinessType(), formDataListDTO.getMenuId(), formDataListDTO.getDistributorMark());
                PaasFormLaneDataEsListVO formDataListVO = stageLaneViewService.topButton(formDataListDTO);
                response = new XbbResponse<>(formDataListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
